import java.util.Comparator;

public class ServerComparator2 implements Comparator<Integer>
{
	private volatile IServerCluster serverCluster = null;
	private volatile IServerClusterUsageData sC_UsageData = null;
	
	private IServer serverObject1 = null;
	private IServer serverObject2 = null;
	
	public ServerComparator2(IServerCluster sC, IServerClusterUsageData sC_UsageData)
	{
		this.serverCluster = sC;
		this.sC_UsageData = sC_UsageData;
	}
	
	@Override
	public int compare(Integer arg0, Integer arg1) 
	{
		int server1_id = arg0.intValue();
		int server2_id = arg1.intValue();
		
		serverObject1 = serverCluster.getServer(server1_id);
		serverObject2 = serverCluster.getServer(server2_id);
		
		if((serverObject1 == null) && (serverObject2 != null))
			return 1;
		else if((serverObject1 != null) && (serverObject2 == null))
			return -1;
		else if((serverObject1 == null) && (serverObject2 == null))
			return 0;
		else
		{
			double powerConsumption1 = serverObject1.getPowerConsumption();
			double powerConsumption2 = serverObject2.getPowerConsumption();
			
			if(powerConsumption1 < powerConsumption2)
				return -1;
			else if(powerConsumption1 > powerConsumption2)
				return 1;
			else
			{
				double serverCapability1 = sC_UsageData.getServerCapability(server1_id);
				double serverCapability2 = sC_UsageData.getServerCapability(server2_id);
				
				if(serverCapability1 < serverCapability2)
					return -1;
				else if(serverCapability1 > serverCapability2)
					return 1;
				else 
					return 0;
			}
		}
	}
}
